package com.Shuang.Test2;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Test {
    public static void main(String[] args) {
        //解析如下字符串放到集合中去
        String userStrs = "10001:张三:男:1990-01-01#10002:李四:女:1989-01-09#10003:王五:男:1999-09-09#" +
                "10004:刘备:男:1899-01-01#10005:孙悟空:男:1900-01-01#10006:张三:女:1999-01-01#" +
                "10007:刘备:女:1999-01-01#10008:张三:女:2003-07-01#10009:猪八戒:男:1900-01-01";
        String[] users = userStrs.split("#");
        List<User> userList = new ArrayList<>();
        for (String userStr : users) {
            String[] userData = userStr.split(":");
            User user = new User();
            String id = userData[0];
            user.setId(Long.valueOf(id));
            String name = userData[1];
            user.setName(name);
            user.setGender(userData[2]);
            user.setBirthday(LocalDate.parse(userData[3]));
            userList.add(user);
        }
        System.out.println(userList);

        //统计上述集合中的各个名字出现次数
        Map<String, Integer> userName = new HashMap<>();
        for (User user : userList) {
            String name = user.getName();
            if (userName.containsKey(name)){
                userName.put(name, userName.get(name) + 1);
            }else{
                userName.put(name, 1);
            }
        }
        userName.forEach((s, n) -> System.out.println(s + ":" + n + "次"));
    }
}
